package com.software.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.software.base.ResultInfo;
import com.software.bean.BannerBean;
import com.software.dto.BannerDto;
import com.software.service.BannerService;
import com.software.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2024-03-02
 */
@Controller
@RequestMapping("/banner")
public class BannerController {


    @Autowired
    private BannerService bannerService;


    /**
     * @description:
     * @author: wyj
     * @date: 2024/3/9 16:55
     * @param:pageNum:当前页码pageSize当前页条目
     * @return:
     */
    @GetMapping("/api/list")
    @ResponseBody
    public ResultInfo getBannerList(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize) {

        Page<BannerBean> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        IPage<BannerBean> bannerBeanIPage = bannerService.queryAllBannerPagination(page);
        return ResultInfo.success((int) bannerBeanIPage.getTotal(), bannerBeanIPage.getRecords());
    }


    /**
     * @description:文件上传
     * @author: wyj
     * @date: 2024/3/9 16:55
     * @param:
     * @return:
     */
    @RequestMapping(value = "/api/uploadImg", method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo uploadFile(BannerDto bannerDto) {
        int result = bannerService.addBanner(bannerDto);
        AssertUtil.isTrue(result != 1, "添加失败");
        return ResultInfo.success();
    }

    /**
     * @description:逻辑删除文件还没删除
     * @author: wyj
     * @date: 2024/3/5 22:27
     * @param:id
     * @return:
     */
    @DeleteMapping("/api/delete/{id}")
    @ResponseBody
    public ResultInfo deleteBanner(@PathVariable Integer id) {
        System.out.println(id);
        int i = bannerService.deleteBannerById(id);
        System.out.println(i);
        if (i == 1) {
            return ResultInfo.success("success");
        } else {
            return ResultInfo.error("删除失败");
        }


    }


    @GetMapping("/front/getBannerImg")
    @ResponseBody
    public ResultInfo getBannerImg() {
        List<BannerBean> bannerBeanList = bannerService.listBySort();
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(200);
        resultInfo.setMsg("success");
        resultInfo.setData(bannerBeanList);
        return resultInfo;
    }

    @GetMapping("/api/getBannerById/{id}")
    @ResponseBody
    public ResultInfo getBannerById(@PathVariable Integer id) {
        BannerBean bannerBean = bannerService.getBannerById(id);
        return ResultInfo.success(bannerBean);
    }

    @PostMapping("/api/update")
    @ResponseBody
    public ResultInfo update(BannerDto  editBannerDto) {
        System.out.println(editBannerDto);
        Integer result =  bannerService.updateBanner(editBannerDto);
        return ResultInfo.success();
    }
}
